﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Services;

public partial class TeamScore : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    List<MatchSchedule> matchSchedules;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {

                bindMatch();
            }
        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTodayMatcheForClientResult> lstResult = new List<GetTodayMatcheForClientResult>();
        lstResult = matchSchedule.GetTodayMatchesForClient();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTeamsByMatchResult> lstResult = new List<GetTeamsByMatchResult>();
        lstResult = matchSchedule.GetTeamsByMatch(Convert.ToInt32(drpMatch.SelectedValue));
        if (lstResult.Count > 0)
        {
            drpTeam.DataSource = lstResult;
            drpTeam.DataTextField = "TeamName";
            drpTeam.DataValueField = "Id";
            drpTeam.DataBind();
            drpTeam.Items.Insert(0, new ListItem("Select Team", "0"));
        }
    }
    [WebMethod]
    public static int SaveTeamScore(int matchScheduleId, int teamId, string run, string over,string wicket)
    {
        IDataRepository _dataObject = new DataRepository();
        Score score;
        score = new Score(_dataObject);
        score.MatchScheduleId = matchScheduleId;
        score.TeamId= teamId;
        score.Run = run + "/" + wicket;
        score.Over = over;        
        List<CheckMatchIdExistResult> lstResult = new List<CheckMatchIdExistResult>();
        lstResult = score.CheckMatchIdExist(matchScheduleId,teamId);
        if (lstResult.Count > 0)
        {
            score.Update();
        }
        else
        {
            score.Create();
        }
        return 1;
    }
}